package com.example.ioc.proxy;

import com.example.ioc.constant.CommonConstant;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class CglibProxy implements MethodInterceptor {

    private Object targetObj;

    public CglibProxy(Object targetObj) {
        this.targetObj = targetObj;
    }

    private String joinPoint= CommonConstant.JOIN_POINT;

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        String name = method.getName();

        if (joinPoint.equals(name)){
            System.out.println("前置增强");
        }
        if (objects!=null){
            for (Object arg : objects) {
                System.out.println("arg参数："+arg);
            }
        }
        Object invoke = method.invoke(targetObj, objects);
        return invoke;
    }
}
